﻿Public Class LABORDER

    Private code = Nothing
    Private name = Nothing
    Private short_name = Nothing
    Private lab_group_code = Nothing
    Private print_barcode = Nothing
    Private tat_limit_day = Nothing
    Private tat_limit_hour = Nothing
    Private tat_limit_min = Nothing
    Private comment = Nothing
    Private STATUS = Nothing
    Private CANCEL_ORDER = Nothing
    Private selectLabOrder = "SELECT a.`code` AS 'รหัส lab order', a.`PRDCODE` AS 'รหัสผลิตภัณฑ์', b.`LAB_GROUP_NAME` AS 'ชื่อกลุ่ม lab (เต็ม)', b.`LAB_GROUP_SHORT_NAME` AS 'ชื่อกลุ่ม lab (ย่อ)', a.`name` AS 'ชื่อการตรวจสอบ lab', a.`short_name` AS 'ชื่อการตรวจสอบย่อ', a.`lab_group_code` AS 'รหัสกลุ่ม lab', a.`print_barcode` AS 'พิมพ์/ไม่พิมพ์ Barcode', a.`tat_limit_day` AS 'เวลาในการรอผล lab/วัน', a.`tat_limit_hour` AS 'เวลาในการรอผล lab/ชั่วโมง', a.`tat_limit_min` AS 'เวลาในการรอผล lab/นาที', c.`CCODE` AS 'CCODE', c.`RCODE` AS 'RCODE', c.`F_PRC` AS 'Finance_PRC', a.`comment` AS 'หมายเหตุ', a.`CANCEL_ORDER` AS 'ยกเลิกการร้องขอ lab นี้', a.`STATUS` AS 'สถานะการเปิดใช้งาน', a.`PRDCAT` AS 'รหัสประเภทผลิตภัณฑ์' FROM lab_order a JOIN lab_group b ON a.lab_group_code = b.LAB_GROUP_CODE JOIN masproduct c ON a.PRDCODE = c.PRDCODE ORDER BY short_name ASC"
    'Private selectMasdrgacc = "SELECT DGTYID, DRGTYPE, convert(bit, [STATUS]) FROM MASDRGTYPE"

    Private PRDCODE = Nothing
    Private CCODE = Nothing
    Private RCODE = Nothing
    Private F_PRC = Nothing

    'Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection

    Property code_() As Integer
        Get
            Return code
        End Get
        Set(value As Integer)
            code = value
        End Set
    End Property

    Property name_() As String
        Get
            Return name
        End Get
        Set(value As String)
            name = value
        End Set
    End Property

    Property short_name_() As String
        Get
            Return short_name
        End Get
        Set(value As String)
            short_name = value
        End Set
    End Property

    Property lab_group_code_() As Integer
        Get
            Return lab_group_code
        End Get
        Set(value As Integer)
            lab_group_code = value
        End Set
    End Property

    Property print_barcode_() As Boolean
        Get
            Return print_barcode
        End Get
        Set(value As Boolean)
            If value = True Then
                print_barcode = 1
            Else
                print_barcode = 0
            End If
        End Set
    End Property

    Property tat_limit_day_() As Integer
        Get
            Return tat_limit_day
        End Get
        Set(value As Integer)
            tat_limit_day = value
        End Set
    End Property

    Property tat_limit_hour_() As Integer
        Get
            Return tat_limit_hour
        End Get
        Set(value As Integer)
            tat_limit_hour = value
        End Set
    End Property

    Property tat_limit_min_() As Integer
        Get
            Return tat_limit_min
        End Get
        Set(value As Integer)
            tat_limit_min = value
        End Set
    End Property

    Property comment_() As String
        Get
            Return comment
        End Get
        Set(value As String)
            comment = value
        End Set
    End Property

    Property STATUS_() As Boolean
        Get
            Return STATUS
        End Get
        Set(value As Boolean)
            If value = True Then
                STATUS = 1
            Else
                STATUS = 0
            End If
        End Set
    End Property

    Property CANCEL_ORDER_() As Boolean
        Get
            Return CANCEL_ORDER
        End Get
        Set(value As Boolean)
            If value = True Then
                CANCEL_ORDER = 1
            Else
                CANCEL_ORDER = 0
            End If
        End Set
    End Property

    Property selectLabOrder_ As String
        Get
            Return selectLabOrder
        End Get
        Set(value As String)

        End Set
    End Property

    Property PRDCODE_() As Integer
        Get
            Return PRDCODE
        End Get
        Set(value As Integer)
            PRDCODE = value
        End Set
    End Property

    Property CCODE_() As Integer
        Get
            Return CCODE
        End Get
        Set(value As Integer)
            CCODE = value
        End Set
    End Property

    Property RCODE_() As Integer
        Get
            Return RCODE
        End Get
        Set(value As Integer)
            RCODE = value
        End Set
    End Property

    Property F_PRC_() As Boolean
        Get
            Return F_PRC
        End Get
        Set(value As Boolean)
            If value = True Then
                F_PRC = 1
            Else
                F_PRC = 0
            End If
        End Set
    End Property

    Public Sub comboboxData(queryCode As String, ByVal comboboxName As ComboBox)
        Dim dataToCombobox As DataTable
        dataToCombobox = connect.GetTable(queryCode)
        Try
            With comboboxName
                .DataSource = dataToCombobox
                .DisplayMember = "b"
                .ValueMember = "a"
                '.BindingContext = BindingContext
            End With
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Public Sub insertLab_orderData()
        Try
            connect = CONDB.NewConnection()
            'connectlocal = conlocal.NewConnection()
            Dim productCode As Integer

            productCode = connect.ExecuteScalar("INSERT INTO masproduct (PRDNAME, PRDCAT, CCODE, RCODE, STATUS, F_PRC) VALUES ('" & name & "', '5', '" & CCODE & "', '" & RCODE & "', '" & STATUS & "', '" & F_PRC & "'); SELECT LAST_INSERT_ID();")

            Dim labOrderCode As Integer
            labOrderCode = connect.GetTable("SELECT MAX(code) + 1 FROM lab_order").Rows(0)(0)

            Dim queryString As String
            queryString = "INSERT INTO lab_order "
            queryString += "(code, PRDCODE, PRDCAT, name, short_name, lab_group_code, print_barcode, tat_limit_day, tat_limit_hour, tat_limit_min, comment, STATUS, CANCEL_ORDER) "
            queryString += "VALUES ("
            queryString += "'" & labOrderCode & "', "
            queryString += "'" & productCode & "', "
            queryString += "'5', "
            queryString += "'" & name & "', "
            queryString += "'" & short_name & "', "
            queryString += "'" & lab_group_code & "', "
            queryString += "'" & print_barcode & "', "
            queryString += "'" & tat_limit_day & "', "
            queryString += "'" & tat_limit_hour & "', "
            queryString += "'" & tat_limit_min & "', "
            queryString += "'" & comment & "', "
            queryString += "'" & STATUS & "', "
            queryString += "'" & CANCEL_ORDER & "')"

            connect.ExecuteNonQuery(queryString)
            'dischargeID = connect.ExecuteScalar("INSERT INTO masdischarge (DISCHARGE, STATUS) VALUES ('" & masdischargeDescription & "', '" & masdischargeStatus & "'); SELECT LAST_INSERT_ID();")
            'connectlocal.ExecuteNonQuery("INSERT INTO MASDISCHARGE (DISCHGID, DISCHARGE, STATUS) VALUES ('" & dischargeID & "', '" & masdischargeDescription & "', '" & masdischargeStatus & "')")
            MsgBox("ท่านได้ทำการเพิ่มข้อมูล lab order เรียบร้อยแล้ว")
        Catch ex As Exception
            MsgBox(vbCrLf & ex.Message)
        End Try
        connect.Dispose()
        'connectlocal.Dispose()
    End Sub

    Public Sub editLab_orderData()
        Try
            connect = CONDB.NewConnection()
            'connectlocal = conlocal.NewConnection()

            Dim queryStringLabOrder As String
            queryStringLabOrder = "UPDATE lab_order SET "
            queryStringLabOrder += "name = '" & name & "', "
            queryStringLabOrder += "short_name = '" & short_name & "', "
            queryStringLabOrder += "lab_group_code = '" & lab_group_code & "', "
            queryStringLabOrder += "print_barcode = '" & print_barcode & "', "
            queryStringLabOrder += "tat_limit_day = '" & tat_limit_day & "', "
            queryStringLabOrder += "tat_limit_hour = '" & tat_limit_hour & "', "
            queryStringLabOrder += "tat_limit_min = '" & tat_limit_min & "', "
            queryStringLabOrder += "comment = '" & comment & "', "
            queryStringLabOrder += "STATUS = '" & STATUS & "', "
            queryStringLabOrder += "CANCEL_ORDER = '" & CANCEL_ORDER & "' "
            queryStringLabOrder += "WHERE code = '" & code & "'"
            connect.ExecuteNonQuery(queryStringLabOrder)
            'MsgBox(queryStringLabOrder)

            Dim queryStringMasproduct As String
            queryStringMasproduct = "UPDATE masproduct SET "
            queryStringMasproduct += "PRDNAME = '" & name & "', "
            queryStringMasproduct += "CCODE = '" & CCODE & "', "
            queryStringMasproduct += "RCODE = '" & RCODE & "', "
            queryStringMasproduct += "STATUS = '" & STATUS & "', "
            queryStringMasproduct += "F_PRC = '" & F_PRC & "' "
            queryStringMasproduct += "WHERE PRDCODE = '" & PRDCODE & "'"
            connect.ExecuteNonQuery(queryStringMasproduct)
            'MsgBox(queryStringMasproduct)

            'connect.ExecuteNonQuery("UPDATE lab_group SET LAB_GROUP_CODE = '" & lab_group_code & "', LAB_GROUP_NAME = '" & LAB_GROUP_NAME & "', LAB_GROUP_SHORT_NAME = '" & LAB_GROUP_SHORT_NAME & "', DIS_ORDER = '" & DIS_ORDER & "', STATUS = '" & STATUS & "' WHERE LAB_GROUP_CODE = '" & lab_group_code & "'")
            'connectlocal.ExecuteNonQuery("UPDATE MASDISCHARGE SET DISCHARGE = '" & masdischargeDescription & "', STATUS = '" & masdischargeStatus & "' WHERE DISCHGID = '" & masdischargeID & "'")
            MsgBox("ท่านได้ทำการแก้ไขข้อมูลกลุ่ม lab เรียบร้อยแล้ว")
        Catch ex As Exception
            MsgBox(vbCrLf & ex.Message)
        End Try
        connect.Dispose()
        'connectlocal.Dispose()
    End Sub
End Class
